package org.apache.ws.secpolicy.model;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.neethi.PolicyComponent;
import org.apache.ws.secpolicy.SP11Constants;
import org.apache.ws.secpolicy.SP12Constants;
import org.apache.ws.secpolicy.SPConstants;
/*
 * OpenID Token Model
 */
 
public class OpenIDToken extends Token {

	private boolean noPassword;
    private boolean hashPassword;
    
	public OpenIDToken(int version){
		setVersion(version);
	}
	
	public QName getName() {
		/*if(version == SPConstants.SP_V12){
			return SP12Constants.OPENID_TOKEN;
		}else{
			return SP11Constants.OPENID_TOKEN;
		}*/
		return SP12Constants.OPENID_TOKEN;
	}

	public PolicyComponent normalize(){
		throw new UnsupportedOperationException();
	}
	
	public void serialize(XMLStreamWriter writer) throws XMLStreamException {
		String localname = getName().getLocalPart();
		String namespaceURI = getName().getNamespaceURI();
		
		String prefix = writer.getPrefix(namespaceURI);
		if(prefix == null){
			prefix = getName().getPrefix();
			writer.setPrefix(prefix, namespaceURI);
		}
		//<sp:OpenIDToken>
		 writer.writeStartElement(prefix, localname, namespaceURI);
		 
		 writer.writeNamespace(prefix, namespaceURI);
		 
		 String inclusion;
		 /*if (version == SPConstants.SP_V12) {
	            inclusion = SP12Constants.getAttributeValueFromInclusion(getInclusion());
	     }else {
	            inclusion = SP11Constants.getAttributeValueFromInclusion(getInclusion()); 
	     }*/
		 inclusion = SP12Constants.getAttributeValueFromInclusion(getInclusion());

		 if(inclusion != null){
			 writer.writeAttribute(prefix, namespaceURI, SPConstants.ATTR_INCLUDE_TOKEN, inclusion);
		 }
		 
		 String pPrefix = writer.getPrefix(SPConstants.POLICY.getNamespaceURI());
		 
		 if (pPrefix == null){
			 writer.setPrefix(SPConstants.POLICY.getPrefix(), SPConstants.POLICY.getNamespaceURI());
		 }
		//<wsp:policy>
		 writer.writeStartElement(prefix, SPConstants.POLICY.getLocalPart(), SPConstants.POLICY.getNamespaceURI());
		 
		 //default token properties
		 if (isDerivedKeys()) {
             writer.writeStartElement(prefix, SPConstants.REQUIRE_DERIVED_KEYS, namespaceURI);
             writer.writeEndElement();  
         } else if (isExplicitDerivedKeys()) {
             writer.writeStartElement(prefix, SPConstants.REQUIRE_EXPLICIT_DERIVED_KEYS, namespaceURI);
             writer.writeEndElement();  
         } else if (isImpliedDerivedKeys()) {
             writer.writeStartElement(prefix, SPConstants.REQUIRE_IMPLIED_DERIVED_KEYS, namespaceURI);
             writer.writeEndElement();  
         }
		 writer.writeEndElement();
		 
		 //</wsp:policy>
		 writer.writeEndElement();
		 
		 //</sp:OpenIDToken>
		 writer.writeEndElement();
	}

	public boolean isNoPassword() {
		return noPassword;
	}
	public void setNoPassword(boolean password){
		this.noPassword = password;
	}

	public boolean isHashPassword() {
		return hashPassword;
	}
	public void setHashPassword(boolean password){
		this.hashPassword = password;
	}
	

}
